
package com.scalar.chiptrack.users;

import com.scalar.chiptrack.utils.AutoGrowingArrayList;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.StringTokenizer;



public class User implements Serializable
{

    public static final String USER_ACTION_VIEW = "view";
    public static final String USER_ACTION_MODIFY = "modify";
    public static final String USER_ACTION_DELETE = "delete";

    private String m_userId = null;
    private String m_password = null;
    private String m_firstName = null;
    private String m_lastName = null;
    private String m_email = null;
    private int m_userGroupId = -1;
    private String m_groupName = null;
    private int m_levelId = -1;
    private int m_addressId = -1;
    private String m_phoneNumber = null;
    private String m_faxNumber = null;
    private HashMap m_functionAccessMap = null;
    private int m_customerId = -1;
    private String m_customerName = null;
    private String m_regionName = null;
    private int m_regionId = -1;
    private String m_expiresOn = null;
    private String m_receiveNotifications = "off";
    private boolean m_isDisabled = false;
    boolean isChecked = false;
    private boolean salesAckMail = false;
    private boolean shipAlerts = false;
    private boolean proformaInvoice = false;
    private boolean packingSlip = false;
    private boolean quoteAckMail = false;
    private int     vendorId    = -1;
    private ArrayList functionAccessList = null;
    private int userType = 0;
    private int salesPersonId = -1;
    private boolean boardKitManagementAccess = false;
    private boolean rmaAckMail = false;
    private String sameAsUserId= null;
    public User() {
		//updated to AutoGrowingArrayList since the latest commons beanutils throwing exception when populating form bean if an indexed property is not found.
        functionAccessList = new AutoGrowingArrayList(FunctionAccess.class);
    }

    public User (String userId, String firstName, String lastName, String email, int groupId, int levelId) {
        m_userId = userId;
        m_firstName = firstName;
        m_lastName = lastName;
        m_email = email;
        m_userGroupId = groupId;
        m_levelId = levelId;
		//updated to AutoGrowingArrayList since the latest commons beanutils throwing exception when populating form bean if an indexed property is not found.
        functionAccessList = new AutoGrowingArrayList(FunctionAccess.class);
    }

    public boolean isExpired() {

        if (m_expiresOn == null) return true;

        boolean expired = true;
        try {
            StringTokenizer st = new StringTokenizer (m_expiresOn, "/");

            int month = Integer.parseInt (st.nextToken());
            int day = Integer.parseInt (st.nextToken());
            int year = Integer.parseInt (st.nextToken());

            Calendar cal = Calendar.getInstance();
            int thisMonth = cal.get (Calendar.MONTH) + 1;
            int today = cal.get (Calendar.DAY_OF_MONTH);
            int thisYear = cal.get (Calendar.YEAR);

            if (year < thisYear) expired = true;
            else if ((month < thisMonth) && (year <= thisYear)) expired = true;
            else if ((day < today) && (month <= thisMonth) && (year <= thisYear)) expired = true;
            else expired = false;
        }
        catch (Exception e) {
            System.out.println ("Exception in user.isExpired (" + m_expiresOn + ") -- " + e);
        }

        return expired;
    }


    public String getUserId(){
        return m_userId;
    }

    public void setUserId (String id) {
        m_userId = id;
    }

    public String getPassword() {
        return m_password;
    }

    public void setPassword (String password) {
        m_password = password;
    }

    public String getFirstName(){
        return m_firstName;
    }
    public void setFirstName (String name) {
        m_firstName = name;
    }

    public String getLastName(){
        return m_lastName;
    }
    public void setLastName (String name) {
        m_lastName = name;
    }

    public String getEmail() {
        return m_email;
    }
    public void setEmail (String email) {
        m_email = email;
    }

    public int getUserGroupId() {
        return m_userGroupId;
    }
    public void setUserGroupId (int id) {
        m_userGroupId = id;
    }

   public int getLevelId() {
        return m_levelId;
    }
    public void setLevelId (int id) {
        m_levelId = id;
    }

    public String getGroupName() {
        return m_groupName;
    }
    public void setGroupName (String groupName) {
        m_groupName = groupName;
    }

    public void setPhoneNumber (String phoneNumber) {
        m_phoneNumber = phoneNumber;
    }
    public String getPhoneNumber() {
        return m_phoneNumber;
    }

    public void setFaxNumber (String faxNumber) {
        m_faxNumber = faxNumber;
    }
    public String getFaxNumber() {
        return m_faxNumber;
    }

    public void setAddressId (int addressId) {
        m_addressId = addressId;
    }
    public int getAddressId() {
        return m_addressId;
    }

    public HashMap getFunctionAccessMap() {
        return m_functionAccessMap;
    }
    public void setFunctionAccessMap (HashMap map) {
        m_functionAccessMap = map;
    }


    public boolean isActionAllowed (String functionName, String action) {

        // ADMIN link is enabled only if the user belongs to ADMIN Group
        if (functionName.equalsIgnoreCase("admin") == true) {

            if ("admin".equalsIgnoreCase(m_groupName) == true) {
                return true;
            }

            return false;
        }

        // make it case insensitive
        functionName = functionName.toLowerCase();

        if (functionName == null) {
            System.out.println ("Function Name is null -- returning false for is" + action + "()");
            return false;
        }

        if (m_functionAccessMap == null) {
            System.out.println ("FunctionAccess Map is null -- returning false for is" + action + "()");
            return false;
        }

        FunctionAccess functionAccess = (FunctionAccess) m_functionAccessMap.get (functionName);
        if (functionAccess == null) {
			//Bhaskar commented this sop statement on 14 july 2003
            //System.out.println ("No FunctionAccess object for \"" + functionName + "\" in the map -- returning false for is" + action + "()");
            //System.out.println ("\n\nfunction MAP in USER Object = " + m_functionAccessMap);
            return false;
        }

        if (action.equalsIgnoreCase (USER_ACTION_VIEW)) {
            return functionAccess.getView();
        }

        if (action.equalsIgnoreCase (USER_ACTION_MODIFY)) {
            return functionAccess.getModify();
        }

        if (action.equalsIgnoreCase (USER_ACTION_DELETE)) {
            return functionAccess.getDelete();
        }

        return false;
    }


    public void setCustomerId (int customerId) {
        m_customerId = customerId;
    }

    public String getRegionName() {
        return m_regionName;
    }

    public void setRegionName (String regionName) {
        m_regionName = regionName;
    }

    public int getRegionId() {
        return m_regionId;
    }

    public void setRegionId (int regionId) {
        m_regionId = regionId;
    }

    // TBD -TBD - TBD - TBD
    public int getCustomerId() {
        return m_customerId;
    }

    public String getCustomerName() {
        return m_customerName;
    }

    public void setCustomerName (String customerName) {
        m_customerName = customerName;
    }

    public String getExpiresOn() {
        return m_expiresOn;
    }

    public void setExpiresOn (String expiresOn) {
        m_expiresOn = expiresOn;
    }

    public String getReceiveNotifications() {
        return m_receiveNotifications;
    }
    public void setReceiveNotifications (String receiveNotifications) {
        m_receiveNotifications = receiveNotifications;
    }


    public boolean getDisabled() {
        return m_isDisabled;
    }

    public void setDisabled (boolean disabled) {
        m_isDisabled = disabled;
    }

    // Balaram, July 8, 2003
    public String getUserName()
    {
        String userFirstName = ( ( m_firstName == null ) ? "" : m_firstName );
        String userLastName = ( ( m_lastName == null ) ? "" : ( " " + m_lastName ) );
        
        return ( userFirstName + userLastName );
    }

    public boolean getIsChecked() {
        return isChecked;
    }

    public void setIsChecked (boolean isChecked) {
       this.isChecked = isChecked;
    }
   public boolean getSalesAckMail()
   {
       return salesAckMail;
   }
   public void setSalesAckMail(boolean salesAckMail)
   {
       this.salesAckMail = salesAckMail;
   }
   public boolean getShipAlerts()
   {
       return shipAlerts;
   }
   public void setShipAlerts(boolean shipAlerts)
   {
       this.shipAlerts = shipAlerts;
   }
   public boolean getProformaInvoice()
   {
       return proformaInvoice;
   }
   public void setProformaInvoice(boolean proformaInvoice)
   {
       this.proformaInvoice = proformaInvoice;
   }
   public boolean getPackingSlip()
   {
       return packingSlip;
   }
   public void setPackingSlip(boolean packingSlip)
   {
       this.packingSlip = packingSlip;
   }

    public int getVendorId() {
        return vendorId;
    }

    public void setVendorId(int vendorId) {
        this.vendorId = vendorId;
    }

    public ArrayList getFunctionAccessList()
    {
        return functionAccessList;
    }

    public void setFunctionAccessList( ArrayList functionAccessList )
    {
        this.functionAccessList = functionAccessList;
    }

    public int getUserType()
    {
        return userType;
    }

    public void setUserType(int userType)
    {
        this.userType = userType;
    }


    public int getSalesPersonId() {
        return salesPersonId;
    }

    public void setSalesPersonId(int salesPersonId) {
        this.salesPersonId = salesPersonId;
    }


    public boolean isQuoteAckMail() {
        return quoteAckMail;
    }

    public void setQuoteAckMail(boolean quoteAckMail) {
        this.quoteAckMail = quoteAckMail;
    }


    public boolean isBoardKitManagementAccess() {
        return boardKitManagementAccess;
    }

    public void setBoardKitManagementAccess(boolean boardKitManagementAccess) {
        this.boardKitManagementAccess = boardKitManagementAccess;
    }


    public boolean isRmaAckMail() {
        return rmaAckMail;
    }

    public void setRmaAckMail(boolean rmaAckMail) {
        this.rmaAckMail = rmaAckMail;
    }

    public String toString() {
        return "\n" + m_userId + " (" + m_firstName + " " + m_lastName + ")\n" +
               "Phone = " + m_phoneNumber + ", Fax = " + m_faxNumber + ", Email = " + m_email +
               "\nGroup ID = " + m_userGroupId + ", Group Name = " + m_groupName +
               "\nLevel ID = " + m_levelId + ", Address ID = " + m_addressId +
               "\nRegion ID = " + m_regionId + ", Region Name = " + m_regionName +
               "\nSales_Ack_Mail = " + salesAckMail + ", Quote_Ack_Mail = " + quoteAckMail;

    }


    public String getSameAsUserId() {
        return sameAsUserId;
    }

    public void setSameAsUserId(String sameAsUserId) {
        this.sameAsUserId = sameAsUserId;
    }
}